package com.cashmama.market.api.application.controller.authentication.handler.vo;

import com.cashmama.market.api.infras.enums.BasicErrorCodeEnum;
import com.cashmama.market.api.infras.exception.ApplicationException;
import com.fqgj.xjd.user.client.enums.UserKycTypeEnum;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Objects;

/**
 * @program: market-api
 * @description
 * @author: frank
 * @create: 2020-01-02 14:07
 **/
public class AadhaarCardSaveRequest implements Serializable {
    private static final long serialVersionUID = -6480220271923581081L;
    private String cardNo;
    private String cardFrontUrl;
    private String cardBackUrl;
    private String handHeldUrl;
    private Integer type;
    private String panCardNo;
    private String panCardUrl;

    public String getPanCardNo() {
        return panCardNo;
    }

    public AadhaarCardSaveRequest setPanCardNo(String panCardNo) {
        this.panCardNo = panCardNo;
        return this;
    }

    public String getPanCardUrl() {
        return panCardUrl;
    }

    public AadhaarCardSaveRequest setPanCardUrl(String panCardUrl) {
        this.panCardUrl = panCardUrl;
        return this;
    }

    public String getCardNo() {
        return cardNo;
    }

    public AadhaarCardSaveRequest setCardNo(String cardNo) {
        this.cardNo = cardNo;
        return this;
    }

    public String getCardFrontUrl() {
        return cardFrontUrl;
    }

    public AadhaarCardSaveRequest setCardFrontUrl(String cardFrontUrl) {
        this.cardFrontUrl = cardFrontUrl;
        return this;
    }

    public String getCardBackUrl() {
        return cardBackUrl;
    }

    public AadhaarCardSaveRequest setCardBackUrl(String cardBackUrl) {
        this.cardBackUrl = cardBackUrl;
        return this;
    }

    public String getHandHeldUrl() {
        return handHeldUrl;
    }

    public AadhaarCardSaveRequest setHandHeldUrl(String handHeldUrl) {
        this.handHeldUrl = handHeldUrl;
        return this;
    }

    public Integer getType() {
        return type;
    }

    public AadhaarCardSaveRequest setType(Integer type) {
        this.type = type;
        return this;
    }

    public void validate(){
        if (StringUtils.isEmpty(cardNo)) {
            throw new ApplicationException("Aadhaar No. can not be empty");
        }
        if (type == null || type == 0) {
            throw new ApplicationException("type is empty");
        }
        if (StringUtils.isEmpty(cardBackUrl)) {
            throw new ApplicationException("Please upload MASKED E-AADHAAR(Back)");
        }
        if (StringUtils.isEmpty(cardFrontUrl)) {
            throw new ApplicationException("Please upload MASKED E-AADHAAR(Front)");
        }
        if (StringUtils.isEmpty(handHeldUrl)) {
            throw new ApplicationException("Please upload Selfie");
        }
        if (StringUtils.isEmpty(panCardNo)) {
            throw new ApplicationException("please enter Pan Card No.");
        }
        if (StringUtils.isEmpty(panCardUrl)) {
            throw new ApplicationException("Please upload PanCard picture");
        }
        if (StringUtils.isNotEmpty(panCardNo) && StringUtils.isNotEmpty(panCardUrl)) {
            String reg = "[a-zA-Z]{3}[P,H][a-zA-Z][0-9]{4}[a-zA-Z]";
            panCardNo = panCardNo.replaceAll("(\\r\\n|\\t|\\\\r|\\\\n\\\\t|\n|\n\t|\r|\r\n|\t)", "");
            boolean matchFlag = panCardNo.matches(reg);
            if (!matchFlag) {
                throw new ApplicationException("Please enter the correct Pan card No.");
            }
        }
        if (Objects.equals(UserKycTypeEnum.AADHAAR.getType(), String.valueOf(type))) {
            String aadhaarReg = "[0-9]{12}";
            cardNo = cardNo.replaceAll("(\\r\\n|\\t|\\\\r|\\\\n\\\\t|\n|\n\t|\r|\r\n|\t)", "");
            if (!cardNo.matches(aadhaarReg)) {
                throw new ApplicationException(BasicErrorCodeEnum.PARAM_VALID_ERROR.getCode(), "Please enter correct AADHAAR NO.");
            }
        }

        if (Objects.equals(UserKycTypeEnum.PASSPORT.getType(), String.valueOf(type))) {
            String passportReg = "[a-zA-Z][0-9]{7}";
            if (!cardNo.matches(passportReg)) {
                throw new ApplicationException(BasicErrorCodeEnum.PARAM_VALID_ERROR.getCode(), "Please enter correct PASSPORT");
            }
        }

        if (Objects.equals(UserKycTypeEnum.VOTER_ID.getType(), String.valueOf(type))) {
            String voterIdReg = "[a-zA-Z]{3}[0-9]{7}";
            if (!cardNo.matches(voterIdReg)) {
                throw new ApplicationException(BasicErrorCodeEnum.PARAM_VALID_ERROR.getCode(), "Please enter correct VOTER ID");
            }
        }
    }
}
